<?php
/**
 * Created by PhpStorm.
 * User: 1
 * Date: 2016-11-24
 * Time: 16:33
 */

namespace app\src\shoppingCart\helper;


use app\src\base\exception\BusinessException; 
use app\src\goods\model\Product;
use app\src\shoppingCart\model\ShoppingCart;

class ShoppingCartCheckHelper
{
    public static function check($item,$buy_count){
//        dump($item);
        if(!isset($item['onshelf'])){
            $item['onshelf'] = 0;
            throw  new BusinessException("该检查信息缺少key:onshelf");
        }
        if(!isset($item['expire_time'])){
            $item['expire_time'] = 0;
            throw  new BusinessException("该检查信息缺少key:expire_time");
        }

        if(!array_key_exists("quantity",$item)){
            throw  new BusinessException("该检查信息缺少key:quantity");
        }

        if(is_null($item['quantity'])){
            $item['quantity'] = 0;
        }

        if($item['onshelf'] == Product::SHELF_OFF){
            return ShoppingCart::CART_STATUS_SHELF_OFF;
        }

        if(isset($item['product_status']) && $item['product_status'] == Product::SHELF_OFF){
            return ShoppingCart::CART_STATUS_SHELF_OFF;
        }
        
        if($item['expire_time'] < time() && $item['expire_time'] > 0){
            return ShoppingCart::CART_STATUS_INVALID;
        } 
        if($item['quantity'] < $buy_count){
            return ShoppingCart::CART_STATUS_INVENTORY_LACK;
        }

        return ShoppingCart::CART_STATUS_NORMAL;

    }
}